package it.giuseppetrisciuoglio.lyrics.manager;

import it.giuseppetrisciuoglio.lyrics.adapter.ArtistaCanzone;
import it.giuseppetrisciuoglio.lyrics.persistence.entity.ParserType;
import it.giuseppetrisciuoglio.lyrics.persistence.entity.Transaction;
import it.giuseppetrisciuoglio.lyrics.persistence.repository.TransactionRepository;
import it.giuseppetrisciuoglio.lyrics.utils.AdapterUtilities;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

public class ScodatoreManager {

	private final transient Logger log = LoggerFactory.getLogger(getClass());

	private int count = -1;

	@Autowired
	private TransactionRepository transactionRepository;

	@Autowired
	private JMSManager jmsManager;

	@Autowired
	private AdapterUtilities adapterUtilities;

	public void send(ArtistaCanzone artistaCanzone) {
		log.debug("send(): artistaCanzone={}, count={}", artistaCanzone, count);
		count++;
		String transactionId = artistaCanzone.getTransactionId();
		switch (count) {
			case 0: {
				if (!sendAndUpdate(transactionId, ParserType.AIRDAVE, artistaCanzone))
					sendRetry(transactionId, artistaCanzone);
				break;
			}
			case 1: {
				if (!sendAndUpdate(transactionId, ParserType.LYRICSKEEPER, artistaCanzone))
					sendRetry(transactionId, artistaCanzone);
				break;
			}
			case 2: {
				if (!sendAndUpdate(transactionId, ParserType.LYRICSMANIA, artistaCanzone))
					sendRetry(transactionId, artistaCanzone);
				break;
			}
			case 3: {
				if (!sendAndUpdate(transactionId, ParserType.LYRICSMUSICA, artistaCanzone))
					sendRetry(transactionId, artistaCanzone);
				break;
			}
			case 4: {
				if (!sendAndUpdate(transactionId, ParserType.LYRICSTIME, artistaCanzone))
					sendRetry(transactionId, artistaCanzone);
				break;
			}
			case 5: {
				if (!sendAndUpdate(transactionId, ParserType.SINGRING, artistaCanzone))
					sendRetry(transactionId, artistaCanzone);
				break;
			}
			case 6: {
				if (!sendAndUpdate(transactionId, ParserType.TEKSTY, artistaCanzone))
					sendRetry(transactionId, artistaCanzone);
				break;
			}
			case 7: {
				if (!sendAndUpdate(transactionId, ParserType.WIKITESTI, artistaCanzone))
					sendRetry(transactionId, artistaCanzone);
				break;
			}
			case 8: {
				if (!sendAndUpdate(transactionId, ParserType.LYRICSMODE, artistaCanzone))
					sendRetry(transactionId, artistaCanzone);
				break;
			}
			case 9: {
				if (!sendAndUpdate(transactionId, ParserType.WIKIALYRICS, artistaCanzone))
					sendRetry(transactionId, artistaCanzone);
				count = -1;
				break;
			}
			default: {
				sendRetry(transactionId, artistaCanzone);
				count = -1;
			}
		}

	}

	private boolean sendAndUpdate(String transactionId, ParserType parserType, ArtistaCanzone artistaCanzone) {
		log.debug("sendAndUpdate(): transactionId={}, parserType={}", transactionId, parserType);
		boolean isSend = false;
		Transaction transactionDB = transactionRepository.findByCodeAndParserType(transactionId, parserType);
		if (transactionDB == null) {
			log.debug("sendAndUpdate(): Processing transaction with id={} and parseType={}", transactionId, parserType);
			String xmlArtistaCanzone = adapterUtilities.createXMLString(artistaCanzone);
			log.debug("sendAndUpdate(): xml={}", xmlArtistaCanzone);
			jmsManager.sendJMS(parserType, xmlArtistaCanzone);
			isSend = true;
		} else {
			log.info("sendAndUpdate(): Transaction with id={} and parseType={} already processing.", transactionId, parserType);
			isSend = false;
		}
		return isSend;
	}

	public void sendRetry(String transactionId, ArtistaCanzone artistaCanzone) {
		log.debug("sendRetry(): transactionId={}, artistaCanzone={}", transactionId, artistaCanzone);
		jmsManager.sendRetry(artistaCanzone);
		log.debug("sendRetry(): end.");
	}

}
